package com.sense.kukoo.biz_media.adapter

import com.sense.kukoo.common.media.bean.LocalMediaListItemBean
import com.sense.kukoo.common.repo.bean.MediaItemBean
import com.sense.kukoo.common.wiget.adapter.MultiItemAdapter
import com.sense.kukoo.biz_media.adapter.provider.MediaItemProvider

class MediaAdapter(
    data: ArrayList<MediaItemBean>,
    val listener: MediaListener,
):
    MultiItemAdapter<MediaItemBean>(data) {

    private val TAG: String = javaClass.simpleName
    private var mCurrentMusic: LocalMediaListItemBean? = null

    private val mProvider: MediaItemProvider = MediaItemProvider(listener)

    init {
        addItemProvider(mProvider)
    }

    override fun getItemType(data: MutableList<MediaItemBean>, position: Int): Int {
        return data.get(position).type
    }

    interface MediaListener{
        fun onItemClick(bean:MediaItemBean, position: Int)
        fun onMediaPlayClick(bean:MediaItemBean, position: Int)
        fun onMediaToList(bean:MediaItemBean, position: Int)
        fun onMediaDelete(bean:MediaItemBean, position: Int)
        fun onTouchChanged(isTouching:Boolean)
    }

    fun setCurrentMusic(music: LocalMediaListItemBean){
        mCurrentMusic = music
        mProvider.setCurrentMusic(music)
        notifyDataSetChanged()
    }

    fun setCurrentPlayState(state: Int, progress: Int){
        mProvider.setCurrentPlayState(state, progress)
        var position = -1
        list.forEachIndexed { index, mediaItemBean ->
            if(mediaItemBean.data.idOfChapter() == mCurrentMusic?.idOfChapter()){
                position = index
            }
        }
        if (position==-1) {
            notifyDataSetChanged()
        }else{
            notifyItemChanged(position)
        }

    }
}